﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Game.Frames;
using System.IO;
using System.Drawing.Text;
using Game.Klassen;
using System.Media;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Game
{
    public partial class controlMenu : UserControl
    {
        #region Initialisieren

        //Eigenschaften
        Main    fenster;
        bool    renderEnabled = true;
        List<BackGroundObject> bgObjects;

        //Grafik zum Zeichnen
        Graphics g;
        Image    bm;

        //Letzte Mausposition
        int lastMousePos = 0;
   
        //Konstruktor
        public controlMenu(Main fenster)
        {
            //Hauptfenster setzen
            InitializeComponent();
            this.fenster = fenster;

            //Image und Graphics init
            bm  = (Image)new Bitmap(pbBackground.Width, pbBackground.Height);
            g   = Graphics.FromImage(bm);

            //bgObjects init
            bgObjects = new List<BackGroundObject>();
            for(int x = 0; x < this.Width; x++)
                if (x % 100 == 0)
                {
                    bgObjects.Add(new Bubble(new Point(x, Session.rand.Next(0,800))     , new Size(40, 40), true, "left"));
                    bgObjects.Add(new Bubble(new Point(x, Session.rand.Next(100, 800))  , new Size(20, 20), true, "right"));
                }

            //Hintergrundmusik abspielen
            if (Session.soundEnabled)
                fenster.playMenuMusic();
            else
                lbSound.Image = Game.Properties.Resources.sound_disabled;

            
            //Schriftart festlegen
            Session.loadFont();
            btCredits.Font  = Session.buttonFont;
            btEditor.Font   = Session.buttonFont;
            btExit.Font     = Session.buttonFont;
            btPlay.Font     = Session.buttonFont;
            btTutorial.Font = Session.buttonFont;
        }
        #endregion

        #region Events
        //Credits anzeigen
        private void btCredits_Click(object sender, EventArgs e)
        {
            fenster.showCredits();
        }

        //Ton Umschalten
        private void lbSound_Click(object sender, EventArgs e)
        {
            if (Session.soundEnabled)
            {
                fenster.stopGameMusic();
                Session.soundEnabled = false;
                lbSound.Image = Game.Properties.Resources.sound_disabled_hover;
            }
            else
            {
                fenster.playMenuMusic();
                Session.soundEnabled = true;
                lbSound.Image = Game.Properties.Resources.sound_enabled_hover;
            }
        }

        //Button Events
        private void pbBackground_Click(object sender, EventArgs e)
        {
            renderEnabled = true;
        }

        private void btEditor_Click(object sender, EventArgs e)
        {
            fenster.showEditor(new Size(20, 20));
            renderEnabled = false;
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btPlay_Click(object sender, EventArgs e)
        {
            fenster.showLevelAuswahl();
        }


        //Button Hover
        private void btHover_MouseEnter(object sender, EventArgs e)
        {
            ((Button)sender).Image = Game.Properties.Resources.button_hover;
        }

        private void btHover_MouseLeave(object sender, EventArgs e)
        {
            ((Button)sender).Image = Game.Properties.Resources.button;
        }

        //Sound Button Hover
        private void btSound_MouseEnter(object sender, EventArgs e)
        {
            if (Session.soundEnabled)
                lbSound.Image = Game.Properties.Resources.sound_enabled_hover;
            else
                lbSound.Image = Game.Properties.Resources.sound_disabled_hover;
        }

        private void btSound_MouseLeave(object sender, EventArgs e)
        {
            if (Session.soundEnabled)
                lbSound.Image = Game.Properties.Resources.sound_enabled;
            else
                lbSound.Image = Game.Properties.Resources.sound_disabled;
        }

        //Letzte Mausposition abfangen
        private void pbBackground_MouseMove(object sender, MouseEventArgs e)
        {
            lastMousePos = ((Control)sender).Location.X + e.X;
        }
        #endregion

        //Rendering Mainloop
        private void mainloop_Tick(object sender, EventArgs e)
        {
            if (renderEnabled)
            {
                //Hintergrund blau malen
                g.Clear(Color.FromArgb(0, 150, 220));

                //Hintergrundebene zeichnen
                g.DrawImage(Game.Properties.Resources.menuBG, -1 * (lastMousePos / 60), 0, 1024, 768);

                //Hintergrundobjekte malen
                foreach (BackGroundObject obj in bgObjects)
                {
                    obj.Update();
                    g.DrawImage(obj.getImage(), obj.position.X + obj.offset.X, obj.position.Y + obj.offset.Y, obj.size.Width, obj.size.Height);
                }

                //Vordergrund Ebenen zeichnen
                g.DrawImage(Game.Properties.Resources.shine , -1 * (lastMousePos / 40), 0, 1024, 768);
                g.DrawImage(Game.Properties.Resources.menuFG, -1 * (lastMousePos / 20), 0, 1024, 768);

                //Logo Malen
                g.DrawImage(Game.Properties.Resources.font_riseandfall, 200 + (lastMousePos / 100), 50, 625, 250);

                //Button verschiebung
                btCredits.Location  = new Point(350 + (lastMousePos / 100), btCredits.Location.Y);
                btEditor.Location   = new Point(350 + (lastMousePos / 100), btEditor.Location.Y);
                btExit.Location     = new Point(350 + (lastMousePos / 100), btExit.Location.Y);
                btPlay.Location     = new Point(350 + (lastMousePos / 100), btPlay.Location.Y);
                btTutorial.Location = new Point(350 + (lastMousePos / 100), btTutorial.Location.Y);
                lbSound.Location    = new Point(910 + (lastMousePos / 100), lbSound.Location.Y);

                //Picturebox Update
                pbBackground.Image = bm;
            }
        }

        private void btTutorial_Click(object sender, EventArgs e)
        {
            Session.fenster.showTutorial();
        }
    }
}
